

#import "CalendarListViewController.h"
#import "IDominoAppDelegate.h"
#import "CalendarListTableViewCell.h"

@implementation  CalendarListViewController
@synthesize indexDates;
@synthesize indexedCalendarDict;
@synthesize calendarStartDate;
@synthesize calendarEndDate;

- (id)initWithStyle:(UITableViewStyle)style {
	if (self = [super initWithStyle:style]) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.rowHeight = 48;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableView.sectionHeaderHeight =20;
		self.title=NSLocalizedString(@"Calendar", @"Calendar Title");
		self.tabBarItem.image=[UIImage imageNamed:@"TabCalendarView.png"];
		self.hidesBottomBarWhenPushed=NO;

		IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];

		UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemRefresh
																					 target:appDelegate action:@selector(refreshAction:)];
		self.navigationItem.leftBarButtonItem=refreshItem;
		[refreshItem release];
		UIBarButtonItem *moreCalendarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"More", @"") 
																			 style:UIBarButtonItemStylePlain target:self action:@selector(moreCalendarAction:)];
		self.navigationItem.rightBarButtonItem=moreCalendarItem;
		[moreCalendarItem release];
		
		indexDates=[[NSMutableArray alloc] init];
		indexedCalendarDict=[[NSMutableDictionary alloc] init];

	}
	return self;
}

- (void)dealloc
{
	[indexDates release];
	[indexedCalendarDict release];
	[super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	if([indexDates count] == 0 )
		return 1;
	else
		return [indexDates count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if([indexDates count]==0)
		return 1;
	NSString* date=[indexDates objectAtIndex:section];
	NSMutableArray* array=[indexedCalendarDict objectForKey:date];
	return [array count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if([indexDates count] == 0){
		return nil;
	}
	return [indexDates objectAtIndex:section];
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{	
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
	if([indexDates count]==0)
		return ;
	IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	[appDelegate configSegment:2];
	if((indexPath.section==0) && (indexPath.row ==0))
		[appDelegate configSegment:0];
	if((indexPath.section== ([self numberOfSectionsInTableView:tableView]-1)) &&
			(indexPath.row==([self tableView:tableView numberOfRowsInSection:indexPath.section]-1))) 	
		[appDelegate configSegment:1];
	
	[appDelegate showCalendarInfo:[(CalendarListTableViewCell *)[tableView cellForRowAtIndexPath:indexPath] calendar]];
	currentIndexPath=indexPath;
	return ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	static NSString *MyIdentifier = @"CalendarViewCell";
    
  	CalendarListTableViewCell *cell = (CalendarListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[CalendarListTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
    
    // Set up the cell.
    
	if ([indexDates count]==0) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DefaultTableViewCell"] autorelease];
		NSDate* startDate=[NSDate dateWithTimeIntervalSinceNow:60*60*24*calendarStartDate];
		NSDate* endDate=[NSDate dateWithTimeIntervalSinceNow:60*60*24*calendarEndDate];
		NSDateFormatter* transfer=[[NSDateFormatter alloc] init];
		[transfer setDateFormat:@"MMMM d"];
		NSString* start=[transfer stringFromDate:startDate];
		NSString* end=[transfer stringFromDate:endDate];
		[transfer release];
		cell.textLabel.text=[NSString stringWithFormat:@"%@ from %@ to %@", NSLocalizedString(@"N-A", @"Not Available"),start, end];
		cell.textLabel.textColor = [UIColor colorWithWhite:0.5 alpha:0.5];
		cell.textLabel.font=[UIFont systemFontOfSize:16];
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle=UITableViewCellSelectionStyleNone;
		return cell;
	}
	NSString* date=[indexDates objectAtIndex:indexPath.section];
	NSArray* array=[indexedCalendarDict objectForKey:date];	
	[cell setCalendar:[array objectAtIndex:indexPath.row]];
	return cell;
}


- (void)setUpDisplayList:(NSArray*)calendarEntryArray{
	[indexDates removeAllObjects];
	[indexedCalendarDict removeAllObjects];

	for (Calendar *calendar in calendarEntryArray) {
		NSDateFormatter* transfer=[[NSDateFormatter alloc] init];
		[transfer setDateFormat:@"  dd-MM-yy                                 E"];
		NSString *date = [transfer stringFromDate:[calendar beginTime]];
		[transfer release];
		if(date==nil)
			date=@"no beginTime";
		NSMutableArray *indexArray = [indexedCalendarDict objectForKey:date];
		if (indexArray == nil) {
			indexArray = [[NSMutableArray alloc] init];
			[indexedCalendarDict setObject:indexArray forKey:date];
			[indexArray release];
		}
		[indexArray addObject:calendar];
		if(![indexDates containsObject:date])
			[self.indexDates addObject:date];
	}
	[self.tableView reloadData];
}


- (Boolean)isCalendarEmpty{
	if([self.indexDates count]==0)
		return YES;
	else
		return NO;
}

- (void)moreCalendarAction:(id)sender
{
	calendarStartDate=calendarStartDate -90;
	calendarEndDate=calendarEndDate+90;	
	IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	[NSThread detachNewThreadSelector:@selector(getCalendarList) toTarget:appDelegate withObject:nil];
}

- (NSIndexPath*)currentIndexPath
{
	return currentIndexPath;
}

@end

